package com.duowei.diandan.fragment.dialog;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.duowei.diandan.R;
import com.duowei.diandan.databinding.PromptDialogFragmentBinding;

/**
 * 提示对话框
 */
public class PromptDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private static final String EXTRA_TITLE = "title";
    private static final String EXTRA_MESSAGE = "message";
    private static final String EXTRA_ONLY_SHOW_OK = "onlyShowOk";
    private static final String EXTRA_CANCEL_BUTTON_TEXT = "cancelButtonText";
    private static final String EXTRA_OK_BUTTON_TEXT = "okButtonText";

    private PromptDialogFragmentBinding mBinding;

    private String mMessage;
    private boolean mOnlyOk = false;
    private String mCancelButtonText;
    private String mOkButtonText;

    /**
     * 提示, 只显示确定按钮
     *
     * @param message    信息
     * @param onlyShowOK true 只显示确定按钮
     */
    public static PromptDialogFragment newInstance(String message, boolean onlyShowOK) {

        Bundle args = new Bundle();
        args.putString(EXTRA_MESSAGE, message);
        args.putBoolean(EXTRA_ONLY_SHOW_OK, onlyShowOK);

        PromptDialogFragment fragment = new PromptDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static PromptDialogFragment newInstanceSettingsSave() {

        Bundle args = new Bundle();
        args.putString(EXTRA_MESSAGE, "重新下载数据?");
        args.putBoolean(EXTRA_ONLY_SHOW_OK, false);
        args.putString(EXTRA_CANCEL_BUTTON_TEXT, "否");
        args.putString(EXTRA_OK_BUTTON_TEXT, "是");

        PromptDialogFragment fragment = new PromptDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mMessage = args.getString(EXTRA_MESSAGE);
            mOnlyOk = args.getBoolean(EXTRA_ONLY_SHOW_OK);
            mCancelButtonText = args.getString(EXTRA_CANCEL_BUTTON_TEXT);
            mOkButtonText = args.getString(EXTRA_OK_BUTTON_TEXT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_prompt_dialog, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mBinding.message.setText(mMessage);

        Button cancelButton = mBinding.btnCancel;
        cancelButton.setOnClickListener(this);
        if (!TextUtils.isEmpty(mCancelButtonText)) {
            cancelButton.setText(mCancelButtonText);
        }

        Button okButton = mBinding.btnOk;
        okButton.setOnClickListener(this);
        if (!TextUtils.isEmpty(mOkButtonText)) {
            okButton.setText(mOkButtonText);
        }

        if (mOnlyOk) {
            mBinding.btnCancel.setVisibility(View.GONE);
            mBinding.centerSpace.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_ok) {
            if (mOnBtnClickListener != null) {
                mOnBtnClickListener.onOkBtnClick();
            }

            dismiss();
        } else if (id == R.id.btn_cancel) {
            if (mOnBtnClickListener != null) {
                mOnBtnClickListener.onCancelBtnClick();
            }

            dismiss();
        }
    }

    private OnBtnClickListener mOnBtnClickListener;

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        mOnBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onCancelBtnClick();

        void onOkBtnClick();
    }
}
